//
//  SWTool.m
//  ShiWu
//
//  Created by  on 2022/7/29.
//
#import "ZZHttp.h"
#import "SWTool.h"
#define planetAPI @"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
#define animalAPI @"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
#define fruitAPI @"https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient"
#define otherAPI @"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
@implementation SWTool
+ (NSURLSessionTask *)swIMG:(UIImage *)IMG type:(ShiWuType)type success:(returnBlock)block
{
    [CCHud startHud:LS(@"识别中...")];
    [ZZHttp http].basicURLString = @"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=giAjuO6C1U0VyB7x9S28SpMy&client_secret=cSW7p3RvF1YIE2treeDBMCpzVSWseFdy";
    [ZZHttp http].type = CCRequestContentTypeJson;
    return [ZZHttp post:@"" para:nil success:^(id  _Nonnull response) {
        NSString *token = [response valueForKey:@"access_token"];
        if (token != nil) {
            NSString *base64 = [self URLEncodedString:[IMG base64]];
            [self BAIDU:token base64:base64 type:type success:block];
        }else{
            [CCHud startHud:LS(@"获取token失败") stopTime:0.8 handle:^{
                
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"");
        [CCHud startHud:LS(@"识别失败") stopTime:0.8 handle:^{
            
        }];

    }];
}
+ (void)BAIDU:(NSString *)token base64:(NSString *)base64 type:(ShiWuType)type success:(returnBlock)block
{
    [ZZHttp http].type = CCRequestContentTypeForm;
    if (type == ShiWuTypePlanet) {
        [ZZHttp http].basicURLString = planetAPI;
    }else if (type == ShiWuTypeAnimal){
        [ZZHttp http].basicURLString = animalAPI;
    }else if (type == ShiWuTypeFruit){
        [ZZHttp http].basicURLString = fruitAPI;
    }else{
        [ZZHttp http].basicURLString = otherAPI;
    }
    [ZZHttp http].shiwutype = type;
    [ZZHttp post:@"" para:@{@"access_token":token,@"image":base64,@"baike_num":@"1"} success:^(id  _Nonnull response) {
        NSLog(@"response = %@",response);
        if ([response valueForKey:@"result"] != nil) {
            NSArray *array = [response valueForKey:@"result"];
            block((NSDictionary *)array[0]);
            [CCHud startHud:LS(@"识别成功") stopTime:0.8 handle:^{
                
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"");
        [CCHud startHud:LS(@"识别失败") stopTime:0.8 handle:^{
            
        }];
    }];
}

+ (NSString *)URLEncodedString:(NSString *)string {
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)string,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}
@end
